package iframe;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.ErrorHandler.UnknownServerException;

import com.sun.jmx.snmp.Timestamp;

/**
 * @author Sadaiyandi
 *
 */
public class MyIframe {

	public WebDriver driver;
	private String baseUrl,SourceId;

	public static void main(String[] args){
		MyIframe mif = new MyIframe();
		mif.launch_browser(args);
	}
	/**
	 * @param Partys
	 */
	public void launch_browser(String[] Partys) {
		PrintWriter log = null;
		try{
			
			try {
				log = new PrintWriter(new File("C:\\Users\\QA Admin\\Downloads\\selenium\\tutor\\log"+new Timestamp().getDate().toString().replaceAll(":", "-")+".html"));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				driver.quit();
				e.printStackTrace();
			}
			driver = new FirefoxDriver();
			log.println("<html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /> <title>Untitled Document</title> </head>  <body bgcolor=\"#99CCFF\"  > <H2>DSUI Automation Testing Report</H2> <br/>");
			for(int i1=0;i1<Partys.length;i1++)
			{
			SourceId=Partys[i1];
			baseUrl = "https://192.168.1.219:9444/CustomerDataStewardshipWeb/";        
			
			driver.get(baseUrl);
			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			
			
			
			log.println("Entering the URL: ");
			log.println(driver.getCurrentUrl());
			List<WebElement> frms= driver.findElements(By.tagName("iframe"));  
			System.out.println(frms.size());
			for(int i=0;i<frms.size();i++)
			{
				System.out.println(frms.get(i).getAttribute("src"));
			}
			
			WebElement Party_maintaince = driver.findElement(By.xpath("//iframe[@src='/CustomerDataStewardshipWeb/faces/initNavigationVertical.jsp']"));
			driver.switchTo().frame(Party_maintaince);

			WebElement Partymaintaince = driver.findElement(By.id("form1:text2"));
			log.println("</br>Clicking   "+Partymaintaince.getText());
			Partymaintaince.click();

			WebElement Searchparty = driver.findElement(By.id("form1:text5"));
			log.println("</br>Clicking   "+Searchparty.getText());
			Searchparty.click();


			driver.switchTo().defaultContent();


			driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			WebElement Search_party = driver.findElement(By.xpath("//iframe[@src='/CustomerDataStewardshipWeb/faces/search/partySearch.jsp']"));
			driver.switchTo().frame(Search_party);

			WebElement Partyid = driver.findElement(By.name("form1:partyid"));
			Partyid.sendKeys(SourceId);
			log.println("</br>Entering Partyid into : "+Partyid.getText()+SourceId);
			log.println("</br>Search Party Screen info</br>");
			List<WebElement> input= driver.findElements(By.tagName("input"));  
			System.out.println(input.size());
			log.println("<table border=\"5\" align=\"center\"  bordercolor=\"maroon\" bordercolorlight=\"#8080c0\" bordercolordark=\"#400080\" bgcolor=\"#ffffcc\"><tr bgcolor=\"#ccffcc\">");

			log.println("<td>id</td>");
			log.println("<td>class</td>");
			log.println("<td>name</td>");
			log.println("<td>size</td>");
			log.println("<td>value</td></tr>");
			for(int i=0;i<input.size();i++)
			{

				// System.out.println(input.get(i).getAttribute("id"));
				if(!input.get(i).getAttribute("type").equalsIgnoreCase("hidden"))
				{
					log.println("<tr>");
					log.println("<td>"+input.get(i).getAttribute("id")+"</td>");
					log.println("<td>"+input.get(i).getAttribute("class")+"</td>");
					log.println("<td>"+input.get(i).getAttribute("name")+"</td>");
					log.println("<td>"+input.get(i).getAttribute("size")+"</td>");
					log.println("<td>"+input.get(i).getAttribute("value")+"</td>");
					log.println("</tr>");
				}

			}
			log.println("</table>");

			driver.findElement(By.name("form1:button_Retrieve")).click();
			log.println("</br>Clicking  Retrieve to get the <b>Party Details</b>  with SourceId: "+SourceId);
			//Thread.sleep(9000);
			// driver.switchTo().defaultContent();
			//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			List<WebElement> outputText= driver.findElements(By.className("outputText"));  
			System.out.println(input.size());
			log.println("</br><table border=\"5\" align=\"center\" bordercolor=\"#677fff\" bordercolorlight=\"silver\" bordercolordark=\"#400080\" bgcolor=\"#ffffcc\"><tr bgcolor=\"#ffcccc\">");
			log.println("<td>id</td>");
			log.println("<td>value</td></tr>");
			for(int i=0;i<outputText.size();i++)
			{

				// System.out.println(input.get(i).getAttribute("id"));
				if(outputText.get(i).getAttribute("style").isEmpty() )
				{

					log.println("<tr>");
					log.println("<td>"+outputText.get(i).getAttribute("id")+"</td>");
					log.println("<td>"+outputText.get(i).getText()+"</td>");
					log.println("</tr>");

				}

			}
			log.println("</table>");
			
			String PartyId=driver.findElement(By.id("form1:PersonName_id_value")).getText();
			log.println("</br>Retrieved Party Info Successfully");
			if(SourceId.equals(PartyId))
			{
				log.println(" </br>Search Completed Successfully for the Party  with PartyId: "+PartyId);
				System.out.println("Search Completed Successfully for the Party  with PartyId: "+PartyId);
			}
			
			driver.findElement(By.id("form1:subviewNavigation:panel_NAME_text")).click();
			log.println("</br>Clicking  Retrieve to get the <b>Party Name Details</b>  with SourceId: "+SourceId);
			//Thread.sleep(9000);
			// driver.switchTo().defaultContent();
			//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			List<WebElement> outputText1= driver.findElements(By.className("outputText"));  
			System.out.println(input.size());
			log.println("</br><table border=\"5\" align=\"center\" bordercolor=\"#677fff\" bordercolorlight=\"silver\" bordercolordark=\"#400080\" bgcolor=\"#ffffcc\"><tr bgcolor=\"#ffcccc\">");
			log.println("<td>id</td>");
			log.println("<td>value</td></tr>");
			for(int i=0;i<outputText1.size();i++)
			{

				// System.out.println(input.get(i).getAttribute("id"));
				if(outputText1.get(i).getAttribute("style").isEmpty() )
				{

					log.println("<tr>");
					log.println("<td>"+outputText1.get(i).getAttribute("id")+"</td>");
					log.println("<td>"+outputText1.get(i).getText()+"</td>");
					log.println("</tr>");

				}

			}
			log.println("</table>");
			
			driver.findElement(By.id("form2:subviewNavigation:panel_address__text")).click();
			log.println("</br>Clicking  Retrieve to get the <b>Party Name Details</b>  with SourceId: "+SourceId);
			//Thread.sleep(9000);
			// driver.switchTo().defaultContent();
			//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
			List<WebElement> outputText2= driver.findElements(By.className("outputText"));  
			System.out.println(input.size());
			log.println("</br><table border=\"5\" align=\"center\" bordercolor=\"#677fff\" bordercolorlight=\"silver\" bordercolordark=\"#400080\" bgcolor=\"#ffffcc\"><tr bgcolor=\"#ffcccc\">");
			log.println("<td>id</td>");
			log.println("<td>value</td></tr>");
			for(int i=0;i<outputText1.size();i++)
			{

				// System.out.println(input.get(i).getAttribute("id"));
				if(outputText2.get(i).getAttribute("style").isEmpty() )
				{

					log.println("<tr>");
					log.println("<td>"+outputText2.get(i).getAttribute("id")+"</td>");
					log.println("<td>"+outputText2.get(i).getText()+"</td>");
					log.println("</tr>");

				}

			}
			log.println("</table>");
			
			
			
			
			
			
			
			
			
			
			
			
			
			/* driver.findElement(By.id("ts_last_name")).sendKeys("sarkar");
         driver.switchTo().defaultContent();
         driver.findElement(By.id("ts_in_main")).sendKeys("welcome back");*/
			
			}
		}
		catch(NoSuchElementException ne)
		{
			System.out.println(ne.getLocalizedMessage());
		}
		catch(UnknownServerException u)
		{
			System.out.println(u.getLocalizedMessage());
		}
		driver.quit();
		log.println("</body></html>");
		log.close();
	}
	
}